package net.test.dao.impl;

import net.test.bean.Pager;
import net.test.dao.AdminDao;
import net.test.entity.Admin;
import net.test.util.StringUtil;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository("adminDaoImpl")
public class AdminDaoImpl extends BaseDaoImpl<Admin, String> implements AdminDao{

	@Override
	public Admin getAdminByUsername(String username) {
		String hql = "from Admin as admin where lower(admin.username) = lower(:username)";
		return (Admin) getSession().createQuery(hql).setParameter("username", username).uniqueResult();
	}
	
	public boolean isExistByUsername(String username) {
		String hql = "from Admin as admin where lower(admin.username) = lower(:username)";
		Admin admin = (Admin) getSession().createQuery(hql).setParameter("username", username).uniqueResult();
		if (admin != null) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Pager findPagerByAdmin(Pager pager, Admin admin) {
		Criteria criteria=getSession().createCriteria(Admin.class);
		if(admin!=null){
			if(!StringUtil.isEmpty(admin.getUsername())){
				criteria.add(Restrictions.like("username", admin.getUsername()));
			}
			if(!StringUtil.isEmpty(admin.getName())){
				criteria.add(Restrictions.like("name", admin.getName()));
			}
		}
		
		return findPager(pager, criteria);
	}

	
}
